Everything Totally Explained


Ask & we'll explain, totally!
Java Classloader
Totally Explained


  FOR SALE!Either this or the left-hand panel are available for just $19.95 per
day, or you can have both for only $34.95! Contact us for details.  


View this entry using RSS

Everything about The Java Classloader totally explained

The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system doesn't need to know about files and file systems because of class loaders. Delegation is an important concept to understand when learning about class loaders. A software library is a collection of more or less related object code. In the Java language, libraries are typically packaged in Jar files. Libraries can contain various different sorts of objects, the most important type of object contained in a Jar file is a Java class. A class can be thought of as a named unit of code. The class loader is responsible for locating libraries, reading their contents, and loading the classes contained within the libraries. This loading is typically done "on demand", in that it doesn't occur until the class is actually used by the program. A class with a given name can only be loaded once by a given classloader.

Class loading process

The classloading process is fairly complicated, and is the subject of much confusion in software deployment and development. Java programs may make use of external or third party libraries (that is, libraries written and provided by someone other than the author of the program) or may itself be composed, at least in part, by a number of libraries.
   Each Java class must be loaded by a class loader. When the JVM is started, three class loaders are used:-
  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader
The bootstrap class loader loads the core Java libraries, for example core.jar, server.jar etc. in the /lib directory. This class loader, which is part of the core JVM, is written in native code.
   The extensions class loader loads the code in the extensions directories (/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.
   The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

User-defined class loaders

By default, all user classes are loaded by the default system class loader, but it's possible to replace it by a user-defined (which defaults to the original root system class loader), and even to chain class loaders as desired.
   This makes it possible (for example):
  • to load or unload classes at runtime (for example to load libraries dynamically at runtime, even from a HTTP resource). This is an important feature for:
    • implementing scripting languages,
    • using bean builders,
    • allowing user-defined extensibility
    • allowing multiple namespaces to communicate. This is one of the foundations of CORBA / RMI protocols, for example.
  • to change the way the bytecode is loaded (for example, it's possible to use encrypted Java class bytecode).
  • to modify the loaded bytecode (for example, for load-time weaving of aspects when using Aspect Oriented Programming).

    JAR hell

    JAR hell is a term similar to DLL hell used to describe all the various ways in which the classloading process can end up not working.
  • One case is when a developer or deployer of a Java application has accidentally made two different versions of a library available to the system. This isn't considered an error by the system. Rather, the system will load classes from one or the other library. A developer who thinks he's replaced a library with a new version, but who has instead simply added the new library to the list of available libraries, may be surprised to see the application still behaving as though the old library is in use which, of course, it may well be.
  • Another version of the problem arises when two libraries (or a library and the application) require different versions of the same third library. Without taking extraordinary measures (which may or may not even be available, depending on the circumstances) there's no way to load both versions of the third library.
  • The most complex JAR hell problems arise in circumstances that take advantage of the full complexity of the classloading system. A Java program isn't required to use only a single "flat" classloader, but instead may be composed of several (or, in fact, an indefinite number of) nested, cooperating classloaders. This isn't as uncommon as one might think: so-called "servlet containers" are typically implemented in terms of multiple classloaders. The interactions between classloaders is too complex to be fully described here. It is sufficient to say that classes loaded by different classloaders may interact in ways which may not be fully comprehended by the developer, leading to inexplicable errors or bugs. To remedy the JAR hell problems a Java Community Process - JCP 277 has been initiated. The resolution - Java Module System - intends to introduce a new distribution format, modules versioning scheme and a common modules repository (similar in purpose to Microsoft .NET's Global Assembly Cache).

    Further Information

    Get more info on 'Java Classloader'.


    External Link Exchanges

    Do you know how hard it is to get a link from a large encyclopaedia? Well we're different and will prove it. To get a link from us just add the following HTML to your site on a relevant page:

      <a href="http://java_classloader.totallyexplained.com">Java Classloader Totally Explained</a>

    Then simply click through this link from your web page. Our crawlers will verify your link, extract the title of your web page and instantly add a link back to it. If you like you can remove the words Totally Explained and embed the link in article text.
       As long as your link remains in place, we'll keep our link to you right here. Please play fair - our crawlers are watching. Your site must be closely related to this one's topic. Any kind of spamming, dubious practises or removing the link will result in your link from us being dropped and, potentially, your whole site being banned.



  • Copyright © 2007-8 totallyexplained.com | Licensed under the GNU Free Documentation License | Site Map
    This article contains text from the Wikipedia article Java Classloader (History) and is released under the GFDL | RSS Version